import random
from django.db import models
from Nav.get_web_card import get_website_business_card,randon_image
import uuid
import requests
from django.core.files.base import ContentFile
# Create your models here.
YEAR_IN_SCHOOL_CHOICES = (
    ('linecons-star', 'linecons-star'),
    ('linecons-doc', 'linecons-doc'),
    ('linecons-lightbulb', 'linecons-lightbulb'),
    ('linecons-thumbs-up', 'linecons-thumbs-up'),
    ("linecons-diamond", "linecons-diamond"),
    ("linecons-pencil", "linecons-pencil"),
    ("linecons-user", "linecons-user"),
    ("linecons-heart", "linecons-heart")

)

icons = [
    "linecons-music", "linecons-search", "linecons-mail", "linecons-heart", "linecons-star",
    "linecons-user", "linecons-videocam", "linecons-camera", "linecons-photo", "linecons-attach",
    "linecons-lock", "linecons-eye", "linecons-tag", "linecons-thumbs-up", "linecons-pencil",
    "linecons-comment", "linecons-location", "linecons-cup", "linecons-trash", "linecons-doc",
    "linecons-note", "linecons-cog", "linecons-params", "linecons-calendar", "linecons-sound",
    "linecons-clock", "linecons-lightbulb", "linecons-tv", "linecons-desktop", "linecons-mobile",
    "linecons-cd", "linecons-inbox", "linecons-globe", "linecons-cloud", "linecons-paper-plane",
    "linecons-fire", "linecons-graduation-cap", "linecons-megaphone", "linecons-database",
    "linecons-key", "linecons-beaker", "linecons-truck", "linecons-money", "linecons-food",
    "linecons-shop", "linecons-diamond", "linecons-t-shirt", "linecons-wallet"
]

icon_tuples = ((icon,icon.split("-")[1]) for icon in icons)

class Category(models.Model):
    class Meta:
        ordering = ['-rank']
        verbose_name = "导航分类"
        verbose_name_plural = verbose_name
    farther = models.ForeignKey("self", on_delete=models.CASCADE,blank=True, null=True, verbose_name="父级类别", related_name = "sub_cat")
    name = models.CharField(max_length=100,verbose_name="类别名称")
    icon_name = models.CharField(verbose_name="icon标签", max_length=100,blank=True,null=True, choices=icon_tuples)
    rank = models.IntegerField(verbose_name="分类排序",help_text="数组越大，排序越前", default=0)
    is_show = models.BooleanField(verbose_name="是否展示", default=True)

    def save(self, *args, **kwargs):
        if not self.icon_name:
            icon = random.choice(icons)
            self.icon_name = icon
        super().save(*args, **kwargs)
    def __str__(self):
        return self.name
class ImagesModel(models.Model):
    image = models.ImageField(upload_to="images/",default="img.png", verbose_name="图片地址")
    add_date = models.DateTimeField(auto_now_add=True,verbose_name="添加日期")
    class Meta:
        verbose_name = "图片管理"
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.image.name


class Nav(models.Model):
    class Meta:
        verbose_name = "导航条"
        verbose_name_plural = verbose_name
    category = models.ForeignKey(Category, on_delete=models.CASCADE, verbose_name="所属类别")
    name = models.CharField(max_length=100, verbose_name="地址名称", blank=True, null=True)
    url = models.URLField(verbose_name="导航栏地址")
    description = models.TextField(verbose_name="介绍", blank=True,null=True)
    image = models.ForeignKey(ImagesModel, verbose_name="图片",on_delete=models.SET_NULL, blank=True, null=True)

    permission = models.BooleanField(verbose_name="隐藏显示",default=False,help_text="默认关,开启则在管理员登录下显示")
    def __str__(self):
        if self.name:
            return self.name
        else:
            return "NUll"




